package cn.hzking.gjmall.activitys.my;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.RelativeLayout;

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.hzking.gjmall.R;
import cn.hzking.gjmall.activitys.order.OrderDetailActivity;
import cn.hzking.gjmall.adpters.address.SelectAddressAdapter;
import cn.hzking.gjmall.base.ColorStatuBarActivity;
import cn.hzking.gjmall.bean.address.AddressSimpleBean;
import cn.hzking.gjmall.bean.order.OrderDetailBean;
import cn.hzking.gjmall.net.RetrofitFactory;
import cn.hzking.gjmall.net.base.BaseObserver;
import cn.hzking.gjmall.net.base.GjCustomStateViewObserver;
import cn.hzking.gjmall.utils.ListUtils;
import cn.hzking.gjmall.utils.UIUtils;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public class SelectAddressActivity extends ColorStatuBarActivity implements SelectAddressAdapter.AddressInterface{
    @Bind(R.id.rl_back) RelativeLayout backRl;
    @Bind(R.id.rv) RecyclerView rv;
    private SelectAddressAdapter addressAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_select_address);
        super.onCreate(savedInstanceState);
        ButterKnife.bind(this);
        init();
    }

    private void init() {
        addressAdapter=new SelectAddressAdapter(R.layout.item_selectaddress,null);
        addressAdapter.setAddressInterface(this);
        rv.setLayoutManager(new LinearLayoutManager(this));
        rv.setAdapter(addressAdapter);
        backRl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        getData();
    }

    private void getData() {
        RetrofitFactory.getInstant().API()
                .getAddressList()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<AddressSimpleBean>(this,true) {

                    @Override
                    protected void onSuccees(AddressSimpleBean addressSimpleBean) throws Exception {
                        if(addressSimpleBean!=null){
                            if(!ListUtils.isEmpty(addressSimpleBean.getData().getDeliveryAddressesList())){
                                addressAdapter.setNewData(addressSimpleBean.getData().getDeliveryAddressesList());
                            }
                        }
                    }

                    @Override
                    protected void onFailure(Throwable e, boolean isNetWorkError) throws Exception {
                        if(isNetWorkError){
                            UIUtils.showToastShort(SelectAddressActivity.this,getResources().getString(R.string.net_error));
                        }else {
                            UIUtils.showToastShort(SelectAddressActivity.this,"服务器错误");
                        }
                    }

                    @Override
                    protected void onCodeError(AddressSimpleBean addressSimpleBean) throws Exception {
                        super.onCodeError(addressSimpleBean);
                        UIUtils.showToastShort(SelectAddressActivity.this,addressSimpleBean.getErrorMsg());
                    }
                });
    }

    @Override
    public void select(int addressId) {
        Intent intent = new Intent();
        intent.putExtra("addressId", ""+addressId);
        SelectAddressActivity.this.setResult(RESULT_OK, intent);
        SelectAddressActivity.this.finish();
    }
}
